package cn.zindesign.base.impl;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.ibatis.session.RowBounds;

import cn.zindesign.base.BaseMapper;
import cn.zindesign.base.BaseService;
import cn.zindesign.system.Page;
import cn.zindesign.system.PageList;

/**
 * @author lihui 2012-1-5
 * @param <T>
 */
public class BaseServiceImpl implements BaseService {

	
	@Inject
	@Named("baseMapper")
	private BaseMapper dao;

	/*
	 * 计算查询结果数
	 * 
	 * @see com.zindesign.platform.base.BaseService#countByProperty(java.util.Map)
	 */
	public int countByProperty(String key, Map<?, ?> map) {
		return dao.countByProperty(key, map);
	}

	/*
	 * 删除一条记录
	 * 
	 * @see com.zindesign.platform.base.BaseService#doDeleteById(java.lang.Integer)
	 */
	public int doDeleteById(String key, Integer id) {
		return dao.deleteById(key, id);
	}

	/*
	 * 插入一条记录
	 * 
	 * @see com.zindesign.platform.base.BaseService#doInsert(java.lang.Object)
	 */
	public int doInsert(String key, Object record) {
		return dao.insert(key, record);
	}

	/*
	 * 更新一条记录
	 * 
	 * @see com.zindesign.platform.base.BaseService#doUpdateById(java.lang.Object)
	 */
	public int doUpdateById(String key, Object record) {
		return dao.updateById(key, record);
	}

	/*
	 * 查询一条记录
	 * 
	 * @see com.zindesign.platform.base.BaseService#selectById(java.lang.Integer)
	 */
	public Object selectById(String key, Integer id) {
		return dao.selectById(key, id);
	}

	/*
	 * 根据参数条件查询结果集
	 * 
	 * @see com.zindesign.platform.base.BaseService#selectByProperty(java.util.Map)
	 */
	public List<Object> selectByProperty(String key, Map<?, ?> map) {
		return dao.selectByProperty(key, map);
	}

	/*
	 * 根据参数条件查询带分页的结果集
	 * 
	 * @see com.zindesign.platform.base.BaseService#selectPageListByProperty(java.util.Map)
	 */
	public PageList<Object> selectPageListByProperty(String key, Map<?, ?> map) {
		Page page = (Page) map.get("page");
		if (map.get("page") == null) {
			page = new Page();
		}

		PageList<Object> pl = new PageList<Object>();
		if (page.getTotals() == -1) {
			page.setTotals(dao.countByProperty(key, map));
		}
		page.doPageBreak();
		List<Object> list = dao.selectByProperty(key, map, page.getSpage(),
				page.getPerPageNum());
		pl.setList(list);
		pl.setP(page);
		pl.setPageShowString(page.getListPageBreak());
		return pl;
	}

	/* 
	 * 批量删除
	 * @see cn.zindesign.base.BaseService#doBatchDelete(java.lang.String, java.lang.String[])
	 */
	public int doBatchDelete(String key, String[] ids) {
		int num = 0;
		if(ids!=null&&ids.length>0){
			for(int i=0;i<ids.length;i++){
				num = num + dao.deleteById(key, Integer.parseInt(ids[i]));
			}
		}
		return num;
	}

}
